/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TweetRay;

import Raytracing.Shaders.Material;
import Raytracing.Shaders.Shader;
import Raytracing.Geometry.Sphere;
import java.util.ArrayList;
import processing.core.PApplet;
import processing.core.PGraphics3D;
import processing.core.PVector;

/**
 *
 * @author Matt
 */
public class ThriftySphere extends Entity implements Traceable {

    Sphere sphere;
    float radius;

    public void setRadius(float radius) {
        this.radius = radius;
        tesselate();
    }


    public ThriftySphere(float x, float y, float z, float radius,
            Material[] shaders, int shaderIndex) {
        super(x, y, z);

        this.radius = radius;
        this.shaders = shaders;
        this.shaderIndex = shaderIndex;

        tesselate();
    }

    public void pickDraw(PGraphics3D p3d, int index) {
        p3d.pushMatrix();
        p3d.translate(tx, ty, tz);

        p3d.fill(index);
       
        
        sphere.draw(p3d.parent);
        p3d.popMatrix();
    }

    public void draw(PGraphics3D p3d) {
        p3d.pushMatrix();
        p3d.translate(tx, ty, tz);
        p3d.rotateZ(3.14f);
        if (isSelected) {

            p3d.fill(255, 255, 0);

        } else {
            p3d.fill(sphere.shader.fill);

        }
        sphere.draw(p3d.parent);
        p3d.popMatrix();

    }

    @Override
    public void pack(UTFWriter output) {
        System.out.println("Writing sphere at: " + (int) tx + " : " + (int) ty + " : " + (int) tz);

        output.writeBits((int) tx, 8);
        output.writeBits((int) ty, 8);
        output.writeBits((int) tz, 8);

        output.writeBits((int)radius, 7);
        output.writeBits(0, 7);
        output.writeBits(0, 7);

        output.writeBits(0, 6);
        output.writeBits(0, 6);
        output.writeBits(0, 6);

        output.writeBits(shaderIndex, 4);
    }

    @Override
    public void scale(int axis, float amt) {
        
        if (radius + amt < 255 && radius +amt > 1){
        
        radius += amt;
        sphere.radius += amt;
        }
    }

    @Override
    public Entity copy() {
        return new ThriftySphere(tx, ty, tz, radius, shaders, shaderIndex);
    }
    public void setShader(int shaderIndex) {

        this.shaderIndex = shaderIndex;
        tesselate();

    }
    public ArrayList tesselate() {
        ArrayList ret = new ArrayList();
        sphere = new Sphere(new PVector(tx, ty, tz), radius, (Shader)shaders[shaderIndex]);
        ret.add(sphere);
        return ret;
    }
}
